package com.zyd.blog.personal.service.impl;

import com.zyd.blog.personal.enuminterface.IsDelete;
import com.zyd.blog.persistence.mapper.UserCommentCoMapper;
import com.zyd.blog.persistence.mapper.UserCommentPrMapper;
import com.zyd.blog.personal.service.UserCommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 用户评论业务逻辑层
 * @author mfy
 */
@Service
public class UserCommentServiceImpl implements UserCommentService {

    @Resource
    private UserCommentPrMapper userCommentPrMapper;

    @Resource
    private UserCommentCoMapper userCommentCoMapper;

    @Override
    public List<Map<String,Object>> getAllCommentByProductionId(Long productionId){
        List<Map<String,Object>> result = new ArrayList<>();
        Map<String,Object> condition = new HashMap<>(16);
        condition.put("productionId",productionId);
        condition.put("isDelete", IsDelete.IS_DELETE_NO);
        List<Map<String,Object>> commentPrs = userCommentPrMapper.queryUserCommentPrByCondition(condition);
        for (Map<String,Object> commentPr : commentPrs){
            condition.put("commentId",commentPr.get("id"));
            commentPr.put("comment",userCommentCoMapper.queryUserCommentCoByCondition(condition));
            result.add(commentPr);
        }
        return result;
    }

    @Override
    public Boolean addComment(Long userId,Long productionId,Long commentId,String content,String ch){
        Map<String,Object> condition = new HashMap<>(16);
        condition.put("userId",userId);
        condition.put("content",content);
        condition.put("updateTime",(new Date()).getTime());
        condition.put("addTime",(new Date()).getTime());
        switch (ch){
            case "1":
                condition.put("productionId",productionId);
                return userCommentPrMapper.addUserCommentPr(condition) > 0;
            case "2":
                condition.put("commentId",productionId);
                return  userCommentCoMapper.addUserCommentCo(condition) > 0;
            default :
                break;
        }
        return false;
    }

    @Override
    public Boolean deleteComment(Long userId,Long commentPrId,Long commentCoId,String ch){
        Map<String,Object> condition = new HashMap<>(16);
        condition.put("isDelete",IsDelete.IS_DELETE_YES);
        condition.put("updateTime",(new Date()).getTime());
        switch (ch){
            case "1":
                condition.put("userId",userId);
                condition.put("id",commentPrId);
                return userCommentPrMapper.updateUserCommentPr(condition) > 0;
            case "2":
                condition.put("userId",userId);
                condition.put("id",commentCoId);
                return userCommentCoMapper.updateUserCommentCo(condition) > 0;
            default:
                break;
        }
        return false;
    }

}
